#!/usr/bin/env python

import sys, os
from PyQt4 import QtGui, QtCore

from ecgWidget import *
from gmmWidget import *
from controlWidget import *
from empirica import *

progname = os.path.basename(sys.argv[0])
progversion = "0.1"


class ApplicationWindow(QtGui.QMainWindow):

	def __init__(self):
		QtGui.QMainWindow.__init__(self)
		self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
		self.setWindowTitle("application main window")

		self.file_menu = QtGui.QMenu('&File', self)
		self.file_menu.addAction('&Quit', self.fileQuit,
								 QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
		self.menuBar().addMenu(self.file_menu)

		self.help_menu = QtGui.QMenu('&Help', self)
		self.menuBar().addSeparator()
		self.menuBar().addMenu(self.help_menu)

		self.help_menu.addAction('&About', self.about)
		
		self.statusBar().showMessage("ECG interface", 2000)

		self.initWidgets()

	def fileQuit(self):
		self.close()

	def closeEvent(self, ce):
		self.fileQuit()

	def about(self):
		QtGui.QMessageBox.about(self, "About ", "Electrocardiogram computer interface, v.01")

	def initWidgets(self):
		self.main_widget = QtGui.QWidget(self)
		self.main_widget.setFocus()
		self.setCentralWidget(self.main_widget)

		# layout
		l = QtGui.QGridLayout(self.main_widget)
		l.setSpacing(10)

		# ECG widget
		self.ecg = ECGCanvas(self.main_widget, width=5, height=4, dpi=100, observer=self)
		l.addWidget(self.ecg, 0, 0)

		# GMM widget
		self.gmm = GMMCanvas(self.main_widget, width=5, height=4, dpi=100)
		l.addWidget(self.gmm, 1, 0)

		# control widget
		self.controls = ControlWidget(self.main_widget)
		l.addWidget(self.controls, 0, 1)

		# empirical distribution widget
		self.empirica = Empirica(self.main_widget, width=5, height=4, dpi=100)
		l.addWidget(self.empirica, 1, 1)

	def needOneMoreRR(self):
		'''
		called from ECGCanvas for get new RR value
		'''
		v = self.controls.value / 100.0
		newRR,mode = self.gmm.getSample( v )
		self.controls.setLastRR(newRR)
		self.controls.setLastTossed(mode)
		self.empirica.addData(newRR)
		return newRR


qApp = QtGui.QApplication(sys.argv)

aw = ApplicationWindow()
aw.setWindowTitle("%s" % progname)
aw.show()
sys.exit(qApp.exec_())
#qApp.exec_()
